<?php
/**
 * <https://y.st./>
 * Copyright © 2019 Alex Yst <mailto:copyright@y.st>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org./licenses/>.
**/

$xhtml = array(
	'<{title}>' => 'Processes',
	'<{subtitle}>' => 'Written in <span title="Operating Systems 2">CS 3307</span> by <a href="https://y.st./">Alexand(er|ra) Yst</a>, finalised and <a href="https://y.st./en/coursework/CS3306/Entity-relationship_diagram.xhtml">archived</a> on 2019-04-24',
	'<{copyright year}>' => '2019',
	'takedown' => '2017-11-01',
	'<{body}>' => <<<END
<div class="APA_title_page">
	<p>
		Processes<br/>
		Alexand(er|ra) Yst<br/>
		<span title="Operating Systems 2">CS 3307</span>
	</p>
</div>
<h2><code>top</code></h2>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS3307/top.png" alt="top" class="framed-centred-image" width="661" height="411"/>
<p>
	The <code>top</code> command shows you a list of processes that are currently running, with the ones taking up the top amount of resources appearing at the top of the list.
	You can&apos;t see all the processes that are running this way, but it lets you see which once are hogging resources.
	Personally, I use this command when I can see the $a[CPU] monitor graph on my panel max out and I&apos;m trying to figure out why.
	(For the uninitiated, the panel in Linux is similar to the Windows task bar, except that some people choose to have more than one, often times one at the top of the screen and one at the bottom, each handling different functions.
	You can put them on the sides of the screen too, or (if you want a hard-to-work-with mess, you can put them across the centre of your screen as well.
	They&apos;re really flexible, and can be put anywhere you want them.
	I just have one at the bottom for handling everything, which is the default in Debian with $a[LXDE].)
	Often times when this happens, my laptop is overheating and running very slowly.
	If I can figure out what&apos;s process is causing the problem, I can figure out how to get my laptop running smoothly again.
	So when this happens, I run the <code>top</code> command.
</p>
<p>
	The process using the most $a[CPU] time in my screenshot is the $a[GNOME] screenshot tool, <code>gnome-screenshot</code>.
	I wasn&apos;t running anything $a[CPU]-intensive when I went to take the screenshot.
	I use $a[LXDE], which is lighter weight than $a[GNOME], and taking screenshots is probably already a more $a[CPU]-intensive activity than anything else I was doing at the time.
	I mean, it&apos;s got to capture the colour of each pixel within the boundaries of whatever window I&apos;m trying to take a screenshot of.
	Processes dealing with graphics are going to be more resource-intensive than many things (besides encryption, and other highly-intensive processes).
</p>
<p>
	As for memory use, X.org, <code>Xorg</code>, was using the most $a[RAM] out of everything.
	For anyone that doesn&apos;t know, X.org controls what gets displayed on the monitor.
	Again, it deals with graphics, making it more resource-intensive than many other processes, and my computer was fairly idle at the time I took the screenshot.
	I&apos;m also told X.org is less efficient than its main alternative, Wayland.
	That likely means it uses more resources than is really necessary, again making it no surprise to see it show up in the output of <code>top</code>.
</p>
<p>
	I had nothing causing problems at the moment though, so my memory usage and $a[CPU] usage were both low.
	Lately, when I have a process misbehaving and have to check <code>top</code> for information, it&apos;s my Wi-Fi manager messing up.
	I pretty much assume the Wi-Fi manager&apos;s the problem if I don&apos;t have anything I know should be eating resources active at the moment, and just use <code>top</code> to confirm before I take action.
</p>
<h2>Killing processes</h2>
<p>
	How I kill processes on the command line depends entirely on the situation.
	If the process is currently tying up the command line, hitting control + &quot;c&quot; sends an interrupt signal, ending the currently-active process.
	For example, after running the <code>top</code> command above, I ended the process using this method, because <code>top</code> continues running until you tell it to stop by sending an interrupt signal.
	However, that doesn&apos;t work if the process to be killed is in the background.
</p>
<p>
	Often times, I know the name of the program the process I need to kill is running, and it&apos;s the only process using that program.
	In that case, I use the <code>killall</code> command.
	For example, Firefox used to freeze up on me a lot, and I only had one Firefox instance open.
	To get Firefox to close when it was refusing, I used to run <kbd>killall firefox</kbd> from the command line.
	A game called Minetest used to be a lot less stable than it is these days and also locked up fairly often.
	I didn&apos;t run multiple instances of Minetest at a time, so again, I could simply run <kbd>killall minetest</kbd> to force the Minetest process to end.
	The <code>killall</code> command is a pretty big hammer though.
	It&apos;ll terminate <strong>*all*</strong> processes using that program.
	Sometimes, that&apos;s not what you want.
</p>
<p>
	<code>kill</code> works for killing single processes.
	The catch is that you&apos;ve got to first find the process&apos;s process $a[ID].
	Process $a[ID]s can be seen in the far left column of <code>top</code>&apos;s output, and runaway processes you&apos;d likely want to kill will often be taking up too many resources, so that&apos;s an easy enough way to get the process $a[ID] you need to kill a process using <code>kill</code>.
	For my own use case though, such precision is usually not necessary.
	If you know the name of the program the process is running, <code>pgrep</code> can also be used to find a list of the process $a[ID]s associated with that program.
	From there, you can decide which one is the offensive one that needs to be terminated via <code>kill</code>.
</p>
<p>
	You can&apos;t really screenshot the interrupt signal sent using control + &quot;c&quot;, but if you open a second command line window, you can use <code>kill</code> to end the <code>top</code> process.
</p>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS3307/kill.png" alt="kill" class="framed-centred-image" width="661" height="411"/>
<h2><code>ps</code> and <code>pstree</code></h2>
<p>
	I wasn&apos;t familiar with the <code>ps</code> and <code>pstree</code> commands.
	While I use <code>top</code> often enough to know how it works off-hand, and I kill processes when I need to, <code>ps</code> and <code>pstree</code> aren&apos;t something I make use of in my everyday life, so I had to look them up.
</p>
<h3><code>ps</code></h3>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS3307/ps.png" alt="ps" class="framed-centred-image" width="661" height="411"/>
<p>
	The <code>ps</code> command shows the the processes that are currently running.
	It doesn&apos;t, by default, show all processes though.
	By default, it&apos;ll show only the processes run by the current user that are associated with the current terminal <a href="http://man7.org/linux/man-pages/man1/ps.1.html">(Linux manual page, 2018)</a>.
	Using command line flags, you can have it display other subgroups of running processes too.
	For example, <kbd>ps -auyst</kbd> shows all processes running under the username <code>yst</code> (my user account on the machine), though the list is way too long to take a screenshot of.
</p>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS3307/ps_-auyst.png" alt="ps -auyst" class="framed-centred-image" width="661" height="411"/>
<h3><code>pstree</code></h3>
<img src="/img/CC_BY-SA_4.0/y.st./coursework/CS3307/pstree.png" alt="pstree" class="framed-centred-image" width="661" height="411"/>
<p>
	The <code>pstree</code> command shows the running processes in tree form <a href="http://man7.org/linux/man-pages/man1/pstree.1.html">(Linux manual page, 2016)</a>.
	According to the manual, you can choose any process $a[ID] to use as the root of the tree, but if none is specified, it uses the init daemon.
	Based on what I can see in the output, I believe this means that the tree shows what processes were used to spawn what processes, and the root of the tree is the process all displayed processes were spawned from.
	In my case, it appears <code>systemd</code> is the root displayed, as it&apos;s the init daemon on my system.
</p>
<h2>Conclusion</h2>
<p>
	There are a lot of ways to see what processes are running on your system, focussing on different criteria.
	The <code>pstree</code> command lets you see which processes spawned which other processes in tree form.
	The <code>top</code> command lets you see the top resource hogs, which is helpful when you&apos;re trying to figure out why your computer is performing slowly.
	The <code>ps</code> command gives you several options for which processes you want to see, but by default, shows you the processes you yourself have running within the command line terminal you call it from.
	And occasionally, you may need to kill a process.
	There are several ways to kill processes as well, depending on your use case and your intent.
</p>
<div class="APA_references">
	<h2>References:</h2>
	<p>
		Linux manual page. (2016, June 18). <a href="http://man7.org/linux/man-pages/man1/pstree.1.html">pstree(1) - Linux manual page</a>. Retrieved from <code>http://man7.org/linux/man-pages/man1/pstree.1.html</code>
	</p>
	<p>
		Linux manual page. (2018, August 8). <a href="http://man7.org/linux/man-pages/man1/ps.1.html">ps(1) - Linux manual page</a>. Retrieved from <code>http://man7.org/linux/man-pages/man1/ps.1.html</code>
	</p>
</div>
END
);
